
class TopChart {
  public float x1, x2, y1, y2;
  private float w;
  private float h;
  private ControlP5 control;
  private ListBox lists[];
  private int maxListNum;
  private int listCount;
  private int numOfItem;
  private int itemHeight;
  private int barHeight;
  private color backColor;
  private color activeColor;
  private int space;
  private boolean [] isShow;
  private String [] itemString;
  private int showIndex;
  private int showHeight;
  private boolean isShown;
  private boolean showLists;
  private controlP5.Button updateButton;
  private controlP5.Button yearList;
  
  //Default construction, not used
  TopChart(float X1, float X2, float Y1, float Y2) {
    x1 = X1;
    x2 = X2;
    y1 = Y1;
    y2 = Y2;
    setupParameter();
    setupList();
  }
  
  TopChart( ControlP5 con, float X1, float X2, float Y1, float Y2) {
    x1 = X1;
    x2 = X2;
    y1 = Y1;
    y2 = Y2;
    this.control = con;  
    setupParameter();  
    setupList();
  }
  
  void setupParameter()
  {    
    w= x2-x1;
    h = y2-y1;
    maxListNum = 5;
    listCount =0;
    space = 5;  
    numOfItem =5;  
    isShow = new boolean[maxListNum];
    itemString = new String[maxListNum*numOfItem];
    for( int i = 0 ;i<maxListNum*numOfItem;i++)
    {
      itemString[i]="";
    }
    showIndex = -1;
    isShown = false;
    showLists = true;
  }
  
  private void setupList()
  {
    lists = new ListBox[maxListNum];
    itemHeight =  25;
    barHeight =20;
    backColor =color(255,128);
    activeColor = color(0,0,255,128);
    float tempSpace = space*3;
    updateButton = control.addButton("update",1, int(x1+tempSpace), int(y1+space*2) , int(w/4), barHeight);
    //yearList = control.addButton("yearList" ,2,int(x1+tempSpace+w/2), int(y1+space*2) , int(w/4), barHeight);
    for ( int i = 0 ;i<maxListNum;i++)
    {
        String name = "list"+ Integer.toString(i);
        lists[i] = control.addListBox(name,int(x1+tempSpace), int(y1+space+(i+2)*(barHeight+space) ), int(w-tempSpace*2), barHeight);
        //println("y1+space+(i+1)*(barHeight+space) is "+ (y1+space+(i+1)*(barHeight+space)));
        lists[i].setItemHeight(itemHeight);
        lists[i].setBarHeight(barHeight);
      
        lists[i].captionLabel().toUpperCase(true);
        lists[i].captionLabel().set("PLEASE SELECT GENRE");
        lists[i].captionLabel().style().marginTop = 6;
        lists[i].valueLabel().style().marginTop = 6;
        lists[i].setColorBackground(backColor);
        lists[i].setColorActive(activeColor);
        lists[i].setHeight(showHeight);
        lists[i].hideScrollbar();
        isShow[i] = false;
    }
    lists[maxListNum-1].hide();
    showHeight = 5*(itemHeight);
  }
   
  private void relocate(int index)
  {
    int transY  = 0 ;
    float tempSpace = space*3;
    //If a different list being selected
    if( index != showIndex)
    {
      for( int i =0;i<maxListNum;i++)
      {  
         lists[i].setPosition(int(x1+tempSpace), int(y1+space+(i+2)*(barHeight+space)+transY ));
         lists[i].setOpen(false);
        if( i == index && itemString[i*numOfItem].length()!=0)
        {
          lists[i].setHeight(showHeight);
          lists[i].setOpen(true);
          transY = showHeight;
          isShown = true;
          showIndex =index;
        }
      }
    }
    //If the same list, hide list items
    else
    {
      for( int i =0;i<maxListNum;i++)
      {  
         lists[i].setPosition(int(x1+tempSpace), int(y1+space+(i+2)*(barHeight+space)));
         showIndex =-1;      
         lists[i].setOpen(false); 
      }
      
    }
  }
  
  //New genre is selected
  public void addTopList(int genre)
  {  
    if( genre ==18) //checkbutton "All"
    {
      return; //do nothing;
    }
      //println("genre is "+ genre+", listCount is "+listCount);
      for(int i =listCount; i>0;i--)
      {
        //Copy the content of list from left to right
        copyList(i-1,i);
      }
      lists[0].captionLabel().set(genreName[genre]);  
      String [] ratingStr = dataArray[genre].getRatingString();
      
       for(int i =0;i<numOfItem;i++)
       {
         if( itemString[i].length()!=0)
         {
           //println("itemString["+i+"] is :"+itemString[i]);
           lists[0].removeItem(itemString[i]);
           itemString[i] = "";
         }
       }
       for(int i =0;i<numOfItem;i++)
       {
         if( ratingStr[i].length()!=0)
         {
           //println("ratingStr["+i+"] is: "+ratingStr[i]);
           lists[0].addItem( ratingStr[i],i );
           itemString[i] = ratingStr[i];
         }
       }
       //lists[listCount].show();
    if( listCount < maxListNum)
    {  
       listCount++;           
    }
  }
 
  public void removeTopList(int genre)
  {
    if( genre ==18) //checkbutton "All"
    {
      return; //do nothing;
    }
    if(listCount==0)
    {
      println("Warning: There is no list in listChart!");
      return;
    }
    int n = -1;
    
    //Looking for index of lists
    for(int i = 0 ;i<maxListNum;i++)
    {
       if(lists[i].captionLabel().getText().equals(genreName[genre]) )
       {
           n = i;
           break;
       }
    }
    
    if(n!=-1 && n<maxListNum)  //list exists
    {
      for( int i=n;i<listCount;i++)
      {
        copyList(i+1,i);
      }
      for( int i =0;i<numOfItem;i++)
      {
         lists[listCount].captionLabel().set("PLEASE SELECT GENRE");
         if(itemString[listCount*numOfItem+i].length()!=0)
         {
           lists[listCount].removeItem( itemString[listCount*numOfItem+i] );
           itemString[listCount*numOfItem+i] = "";
         }
      }
      listCount--;
    }
  }
  //Copy the content of list from left to right
  void copyList( int left, int right)
  {
    lists[right].captionLabel().set( lists[left].captionLabel().getText() ); 
    for(int i =0;i<numOfItem;i++)
       {
         //println("itemString["+(right*numOfItem+i)+"] is "+itemString[right*numOfItem+i]);
         if( itemString[right*numOfItem+i].length()!=0)
         {
           lists[right].removeItem( itemString[right*numOfItem+i] );
           itemString[right*numOfItem+i] = "";
         }
       } 
    for(int i =0;i<numOfItem;i++)
       {
         if(itemString[left*numOfItem+i].length()!=0)
         {
           lists[right].addItem( itemString[left*numOfItem+i],i);
           itemString[right*numOfItem+i] = itemString[left*numOfItem+i];
         }
       }
  }
  public void clear()
  {
    for(int i=0;i<maxListNum;i++)
    {
      lists[i].captionLabel().set("PLEASE SELECT GENRE");
      lists[i].setOpen(false);
      showIndex = -1;
      for( int j =0;j<numOfItem;j++)
      {         
         if(itemString[i*numOfItem+j].length()!=0)
         {
           lists[i].removeItem( itemString[i*numOfItem+j] );
           itemString[i*numOfItem+j] = "";
         }
      }
    }
    listCount = 0 ;
  }
   
  void draw() {
   
    //Draw outer frame
    strokeWeight(1);
    noFill();
    stroke(175, 50);
    rectMode(CORNERS);
    rect(x1, y1, x2, y2);
    
    //Draw inner frame
    strokeWeight(1);
    noFill();
    stroke(175, 50);
    rectMode(CORNERS);
    rect(x1+space, y1+space, x2-space, y2/2 - space*6 + 7);    
    
  }
 public int mousePressed()
 {

   float tempSpace = space*3;
   int index = showIndex;
   float transY = 0 ;
   //if checkbox is shown, do nothing
   if(genres.isShown())
   {
     return 0 ;
   }
 
   for(int i = 0 ;i<maxListNum-1;i++)
   {

      if(mouseX>= x1+tempSpace &&  mouseX<= x2-tempSpace && mouseY>=y1+space+(i+2)*(barHeight+space)-barHeight+transY && mouseY<= y1+space+(i+2)*(barHeight+space)+transY )
      {
        //println("ListItem clicked!!"+random(1000) );        
        relocate(i);        
        return 1;
      }
      if( i ==index)
     {
       transY = showHeight;
     }
     
   }
   return 0;
 }
 
 void toggleLists() {
   if (showLists) {  // hide the lists
     for( int i =0;i<maxListNum-1;i++)
         lists[i].hide(); 
         
     showLists = false;
     control.controller("update").hide();
     control.controller("yearList").hide();
   } else {          // show the lists
     for( int i =0;i<maxListNum-1;i++)
         lists[i].show(); 
     showLists = true;
     control.controller("update").show();
     control.controller("yearList").show();
   }
 }
  
  
}//class

